package com.gengzp.permission.factory.impl;

import com.gengzp.permission.core.PermissionExemptUrisFetcher;
import com.gengzp.permission.core.PermissionTokenProcessor;
import com.gengzp.permission.factory.PermissionInstanceFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.gengzp.permission.constants.PermissionBeanNameConstants.TOKEN_PROCESSOR;

/**
 * @ClassName PermissionInstanceFactoryImpl
 * @Description 权限认证框架实例工厂实现类
 * @Author gengzp
 * @Date 2025/8/12 20:59
 */
@Component
public class PermissionInstanceFactoryImpl implements PermissionInstanceFactory {

    @Autowired
    private Map<String, PermissionExemptUrisFetcher> permissionExemptUrisFetcherMap;

    @Autowired
    private Map<String, PermissionTokenProcessor> permissionTokenProcessorMap;

    @Override
    public List<PermissionExemptUrisFetcher> getAllPermissionExemptUrisFetchers() {
        return new ArrayList<>(permissionExemptUrisFetcherMap.values());
    }

    @Override
    public PermissionTokenProcessor getPermissionTokenProcessor() {
        if (permissionTokenProcessorMap.size() > 1) {
            for (PermissionTokenProcessor instance : permissionTokenProcessorMap.values()) {
                if (!instance.getClass().getName().equals(TOKEN_PROCESSOR)) {
                    return instance;
                }
            }
        }
        return permissionTokenProcessorMap.get(TOKEN_PROCESSOR);
    }

}
